package com.example.springboot.mqtt.task;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.example.springboot.mqtt.entity.CashVip;
import com.example.springboot.mqtt.serivice.ICashVibService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class cashVib {

    @Autowired
    private ICashVibService cashVibService;
    @Scheduled(cron = "0/30 * * * * ?")
    public void getCash()
    {
        try {
            CashVip cashVip = cashVibService.getAccount();
            if(cashVip!=null) {
                System.out.println(cashVip);
                String username = cashVibService.getCashVib(cashVip.getUsername(), cashVip.getPassword());
                System.out.println(username);
                //{"code":1000,"data":{"adv":"$17.47","account":"$17.47"},"message":"完成"}
                JSON parse = JSONUtil.parse(username);
                Object code = parse.getByPath("code");
                Object data = parse.getByPath("data");
                if (code.equals(1000)) {
                    JSON parse1 = JSONUtil.parse(data);
                    Object adv = parse1.getByPath("adv");
                    Object account = parse1.getByPath("account");
                    System.out.println(adv);
                    System.out.println(account);
                    if (cashVibService.updateAccount(cashVip.getUsername(), adv.toString(), account.toString())) {
                        System.out.println("更新成功");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
